package com.jiezhuo.controller.jzg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jiezhuo.config.jzg.AuthenticationJss;
import com.jiezhuo.config.jzg.AuthenticationJw;
import com.jiezhuo.config.jzg.AuthenticationJzg;
import com.jiezhuo.config.jzg.AuthenticationXxgl;
import com.jiezhuo.entity.User;
import com.jiezhuo.entity.jzg.UserListVo;
import com.jiezhuo.service.LoginService;
import com.jiezhuo.utils.HttpClientUtil;
import com.jiezhuo.utils.R;
import com.jiezhuo.utils.SslUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@Api(tags = "教职工认证")
@RequestMapping("/authen/")
public class AuthenJzg {
    @Autowired
    private LoginService loginService;
    @Autowired
    AuthenticationJzg authenticationJzg;
    @Autowired
    AuthenticationJss authenticationJss;
    @Autowired
    AuthenticationJw authenticationJw;
    @Autowired
    AuthenticationXxgl authenticationXxgl;



    @GetMapping("getObj")
    public  R a(){
        System.out.println(authenticationJzg);
        return R.ok().data("r","111");
    }
    @GetMapping("jzg")
    public R  authenJzg(String code  ) throws Exception {
        String url=authenticationJzg.getUrl();
        url =url+"?client_id="+authenticationJzg.getClientId()+"&client_secret="+authenticationJzg.getClientSecret()+"&grant_type="+authenticationJzg.getGrantType()+"&redirect_uri="+authenticationJzg.getRedirectUri()+"&code="+code;
        String s="";
        R login =new R();
        try {
             s= HttpClientUtil.doGet(url);
            JSONObject jsonObject = JSON.parseObject(s);
            String string = jsonObject.getString("access_token");
            url =authenticationJzg.getUrlMe()+"?access_token="+string;
            s= HttpClientUtil.doGet(url);
            jsonObject = JSON.parseObject(s);
            User user = new User();
            user.setUserid(jsonObject.getString("employeeNumber").substring(1));
            user.setUserName(jsonObject.getString("employeeNumber").substring(1));
            user.setPassword("Cmedu@2024");
            login= loginService.login(user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(code);
        return  login;
    }
    @GetMapping("jss")
    public R  authenJss(String code  ) throws Exception {
        String url=authenticationJss.getUrl();
        url =url+"?client_id="+authenticationJss.getClientId()+"&client_secret="+authenticationJss.getClientSecret()+"&grant_type="+authenticationJss.getGrantType()+"&redirect_uri="+authenticationJss.getRedirectUri()+"&code="+code;
        String s="";
        R login =new R();
        try {
            s= HttpClientUtil.doGet(url);
            JSONObject jsonObject = JSON.parseObject(s);
            String string = jsonObject.getString("access_token");
            url =authenticationJss.getUrlMe()+"?access_token="+string;
            s= HttpClientUtil.doGet(url);
            jsonObject = JSON.parseObject(s);
            User user = new User();
            user.setUserid(jsonObject.getString("employeeNumber").substring(1));
            user.setUserName(jsonObject.getString("employeeNumber").substring(1));
            user.setPassword("Cmedu@2024");
            login= loginService.login(user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  login;
    }
    @GetMapping("jw")
    public R  authenjw(String code  ) throws Exception {
        String url=authenticationJw.getUrl();
        url =url+"?client_id="+authenticationJw.getClientId()+"&client_secret="+authenticationJw.getClientSecret()+"&grant_type="+authenticationJw.getGrantType()+"&redirect_uri="+authenticationJw.getRedirectUri()+"&code="+code;
        String s="";
        R login =new R();
        try {
            s= HttpClientUtil.doGet(url);
            JSONObject jsonObject = JSON.parseObject(s);
            String string = jsonObject.getString("access_token");
            url =authenticationJw.getUrlMe()+"?access_token="+string;
            s= HttpClientUtil.doGet(url);
            jsonObject = JSON.parseObject(s);
            User user = new User();
            user.setUserid(jsonObject.getString("employeeNumber").substring(1));
            user.setUserName(jsonObject.getString("employeeNumber").substring(1));
            user.setPassword("Cmedu@2024");
            login= loginService.login(user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(code);
        return  login;
    }
    @GetMapping("xxgl")
    public R  authenXxgl(String code  ) throws Exception {
        //获取认证的地址
        String url=authenticationXxgl.getUrl();
        url =url+"?client_id="+authenticationXxgl.getClientId()+"&client_secret="+authenticationXxgl.getClientSecret()+"&grant_type="+authenticationXxgl.getGrantType()+"&redirect_uri="+authenticationXxgl.getRedirectUri()+"&code="+code;
        String s="";
        R login =new R();

        //传入code 获取用户信息
        try {
            s= HttpClientUtil.doGet(url);
            JSONObject jsonObject = JSON.parseObject(s);
            String string = jsonObject.getString("access_token");
            url =authenticationXxgl.getUrlMe()+"?access_token="+string;
            s= HttpClientUtil.doGet(url);
            jsonObject = JSON.parseObject(s);
            User user = new User();
            user.setUserid(jsonObject.getString("employeeNumber").substring(1));
            user.setUserName(jsonObject.getString("employeeNumber").substring(1));
            user.setPassword("Cmedu@2024");
            //做模拟登录
            login= loginService.login(user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(code);
        return  login;
    }


}
